// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2012 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using System.Collections.Generic;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Search;
using DotNetNuke.Entities.Modules;

namespace YourCompany.Modules.DynamicText
{
    using global::DynamicText.Components.Controller;
    using global::DynamicText.Components.DataProvider;

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The Controller class for DynamicText 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class DynamicTextController : ISearchable, IPortable, IDynamicTextController
    {
        private IDataProvider _DataProvider;

        #region Constructors

        public DynamicTextController()
        {
            _DataProvider = DotNetNuke.ComponentModel.ComponentFactory.GetComponent<IDataProvider>();
            if (_DataProvider == null)
            {
                //get the provider configuration based on the type
                String defaultprovider = DotNetNuke.Data.DataProvider.Instance().DefaultProviderName;
                String dataProviderNamespace = "YourCompany.Modules.DynamicText";
                if ("SqlDataProvider".Equals(defaultprovider))
                {
                    _DataProvider = new SqlDataProvider();
                }
                else
                {
                    String providerType = dataProviderNamespace + "." + defaultprovider;
                    _DataProvider =
                        (IDataProvider) DotNetNuke.Framework.Reflection.CreateObject(providerType, providerType, true);
                }
                DotNetNuke.ComponentModel.ComponentFactory.RegisterComponentInstance<IDataProvider>(_DataProvider);
            }
        }

        public DynamicTextController(IDataProvider dataProvider)
        {
            _DataProvider = dataProvider;
        }

        #endregion


        #region "Public Methods"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// gets an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleId">The Id of the module</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public List<DynamicTextInfo> GetDynamicTexts(int ModuleId)
        {

            return CBO.FillCollection<DynamicTextInfo>(_DataProvider.GetDynamicTexts(ModuleId));

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// gets an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleId">The Id of the module</param> 
        /// <param name="ItemId">The Id of the item</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public DynamicTextInfo GetDynamicText(int ModuleId, int ItemId)
        {

            return (DynamicTextInfo)CBO.FillObject(_DataProvider.GetDynamicText(ModuleId, ItemId), typeof(DynamicTextInfo));

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// adds an object to the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="objDynamicText">The DynamicTextInfo object</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public int AddDynamicText(DynamicTextInfo objDynamicText)
        {
            if (objDynamicText.Content.Trim() != "")
            {
                return _DataProvider.AddDynamicText(objDynamicText.ModuleId, objDynamicText.Content, objDynamicText.CreatedByUser);
            }
            return Null.NullInteger;
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// saves an object to the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="objDynamicText">The DynamicTextInfo object</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void UpdateDynamicText(DynamicTextInfo objDynamicText)
        {

            if (objDynamicText.Content.Trim() != "")
            {
                _DataProvider.UpdateDynamicText(objDynamicText.ModuleId, objDynamicText.ItemId, objDynamicText.Content, objDynamicText.CreatedByUser);
            }

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// deletes an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleId">The Id of the module</param> 
        /// <param name="ItemId">The Id of the item</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void DeleteDynamicText(int ModuleId, int ItemId)
        {

            _DataProvider.DeleteDynamicText(ModuleId, ItemId);

        }

        #endregion

        #region "Optional Interfaces"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// GetSearchItems implements the ISearchable Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {

            SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();

            List<DynamicTextInfo> colDynamicTexts = GetDynamicTexts(ModInfo.ModuleID);
            foreach (DynamicTextInfo objDynamicText in colDynamicTexts)
            {
                SearchItemInfo SearchItem = new SearchItemInfo(ModInfo.ModuleTitle, objDynamicText.Content, objDynamicText.CreatedByUser, objDynamicText.CreatedDate, ModInfo.ModuleID, objDynamicText.ItemId.ToString(), objDynamicText.Content, "ItemId=" + objDynamicText.ItemId.ToString());
                SearchItemCollection.Add(SearchItem);
            }

            return SearchItemCollection;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ExportModule implements the IPortable ExportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be exported</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public string ExportModule(int ModuleID)
        {

            string strXML = "";

            List<DynamicTextInfo> colDynamicTexts = GetDynamicTexts(ModuleID);
            if (colDynamicTexts.Count != 0)
            {
                strXML += "<DynamicTexts>";
                foreach (DynamicTextInfo objDynamicText in colDynamicTexts)
                {
                    strXML += "<DynamicText>";
                    strXML += "<content>" + XmlUtils.XMLEncode(objDynamicText.Content) + "</content>";
                    strXML += "</DynamicText>";
                }
                strXML += "</DynamicTexts>";
            }

            return strXML;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ImportModule implements the IPortable ImportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be imported</param> 
        /// <param name="Content">The content to be imported</param> 
        /// <param name="Version">The version of the module to be imported</param> 
        /// <param name="UserId">The Id of the user performing the import</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void ImportModule(int ModuleID, string Content, string Version, int UserId)
        {

            XmlNode xmlDynamicTexts = Globals.GetContent(Content, "DynamicTexts");
            foreach (XmlNode xmlDynamicText in xmlDynamicTexts.SelectNodes("DynamicText"))
            {
                DynamicTextInfo objDynamicText = new DynamicTextInfo();
                objDynamicText.ModuleId = ModuleID;
                objDynamicText.Content = xmlDynamicText.SelectSingleNode("content").InnerText;
                objDynamicText.CreatedByUser = UserId;
                AddDynamicText(objDynamicText);
            }

        }

        #endregion

    }
}